/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.demo.controller;

import com.alipay.api.domain.CertificateQueryInfo;
import com.alipay.api.domain.CertificateReverseResult;
import com.alipay.api.domain.CertificateUseResult;
import com.alipay.api.response.AlipayMarketingCertificateCertificationPrepareuseResponse;
import com.alipay.api.response.AlipayMarketingCertificateUserBatchqueryResponse;
import com.alipay.demo.service.MarketingCertificateService;
import com.alipay.demo.vo.CertificationRefundConfirmCommitRequest;
import com.alipay.demo.vo.CertificationRefundRequest;
import com.alipay.demo.vo.CertificationUseRequest;
import com.alipay.demo.vo.OpenResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jishupei.jsp
 * @version : MarketingCertificateController, v0.1 2024年05月22日 5:27 下午 jishupei.jsp Exp $
 */
@Api(tags = "本地生活凭证模块")
@ApiSupport(order = 4)
@RestController
@RequestMapping(value = "/certificate")
public class MarketingCertificateController {

    @Autowired
    private MarketingCertificateService marketingCertificateService;

    /**
     * 凭证核销准备
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "凭证核销准备")
    @GetMapping(value = "/certification/prepare")
    public OpenResponse<AlipayMarketingCertificateCertificationPrepareuseResponse> certificationPrepareUse(
            @ApiParam(value = "核销二维码下方的12位券码") @RequestParam(required = false) String code,
            @ApiParam(value = "核销二维码解析出来的标识") @RequestParam(required = false) String encryptedData,
            @ApiParam(value = "是否开启跨单核销") @RequestParam(required = false) Boolean crossOrder) {
        return marketingCertificateService.certificationPrepareUse(code, encryptedData, crossOrder);
    }

    /**
     * 凭证核销
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "凭证核销")
    @PostMapping(value = "/certification/use")
    public OpenResponse<List<CertificateUseResult>> certificationUse(@RequestBody CertificationUseRequest request) {
        return marketingCertificateService.certificationUse(request);
    }

    /**
     * 凭证核销状态撤销
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "凭证核销状态撤销")
    @PostMapping(value = "/certification/refund")
    public OpenResponse<List<CertificateReverseResult>> certificationRefund(@RequestBody CertificationRefundRequest request) {
        return marketingCertificateService.certificationRefund(request);
    }

    /**
     * 订单退款前商户回复确认退款结果
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "订单退款前商户回复确认退款结果")
    @PostMapping(value = "/order/refundconfirmcommit")
    public OpenResponse<Void> orderRefundConfirmCommit(@RequestBody CertificationRefundConfirmCommitRequest request) {
        return marketingCertificateService.orderRefundConfirmCommit(request);
    }

    /**
     * 凭证查询
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "凭证查询")
    @GetMapping(value = "/certification/query")
    public OpenResponse<List<CertificateQueryInfo>> certificationQuery(
            @ApiParam(value = "订单号（凭证发放spi入参获取）") @RequestParam(required = false) String orderId,
            @ApiParam(value = "凭证标识ID（通过退款前向商户确认是否可以退款spi入参获取）") @RequestParam(required = false) String certificateIds,
            @ApiParam(value = "凭证归属支付宝用户id或OpenId") @RequestParam String userIdOrOpenId) {
        List<String> certificateIdList = StringUtils.isBlank(certificateIds)
                ? null : Lists.newArrayList(StringUtils.split(certificateIds, ","));
        return marketingCertificateService.certificationQuery(orderId, certificateIdList, userIdOrOpenId);
    }

    /**
     * 凭证分页查询
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "凭证分页查询")
    @GetMapping(value = "/certification/list")
    public OpenResponse<AlipayMarketingCertificateUserBatchqueryResponse> certificationList(
            @ApiParam(value = "页码", defaultValue = "1") Long pageNum,
            @ApiParam(value = "单页大小", defaultValue = "10") Long pageSize,
            @ApiParam(value = "凭证归属支付宝用户id或OpenId") @RequestParam String userIdOrOpenId,
            @ApiParam(value = "凭证状态，UNUSE-未使用，USED-已使用，INVALID-已失效") @RequestParam(required = false) String certificateStatus) {
        return marketingCertificateService.certificationList(pageNum, pageSize, userIdOrOpenId, certificateStatus);
    }
}
